package algorithm;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import operativa.action.utils.LocationProbability;
import operativa.action.utils.LocationRandomFishBins;
import operativa.bean.entity.Location;
import operativa.bean.entity.SimulationConfig;

public class Simulation {
	private SimulationConfig config;
	private ArrayList<LocationProbability> probList = new ArrayList<LocationProbability>();
	private ArrayList<LocationRandomFishBins> ofertList = new ArrayList<LocationRandomFishBins>();
	
	public Simulation(SimulationConfig simulationConfig, ArrayList<LocationProbability> probList, ArrayList<LocationRandomFishBins> ofertList) {
		super();
		this.config = simulationConfig;
		this.probList = probList;
		this.ofertList = ofertList;
	}
	
	public void setAccum(){
		float accum = 0;
		for (LocationProbability locationProb : probList) {
			accum += locationProb.getProbability();
			locationProb.setProbabilityAccum(accum);
			locationProb.getLocation().setFishbins(0);
		}
	}
	
	public void assignDemand(int demand){
		int count = 0;
		for (int i = 0; i < demand; i++) {
			float random = (float) Math.random();
			this.setFishBin(random);
		}
	}

	private void setFishBin(float random) {
		for (LocationProbability locationProb : probList) {
			if(locationProb.getProbabilityAccum().floatValue()>random){
				locationProb.getLocation().setFishbins(locationProb.getLocation().getFishbins().intValue()+1);
				break;
			}
		}
	}
	
	public int getTotalFishBinsFromSelectedHarbors(){
		int total = 0;
		for (LocationRandomFishBins ofert : ofertList) {
			int fishBins = this.getRandom(ofert.getMinFishBins(), ofert.getMaxFishBins());
			ofert.getLocation().setFishbins(new Integer(fishBins));
			total += fishBins;
		}
		return total;
	}
	
	

	
	private int getRandom(int inf, int sup){
		int dif = sup - inf;
		double random = Math.random();
		double result = (random * dif);
		result = result + inf;
		return (int)result; 
	}
//	
//	private int getAverage(){
//		int sum = 0;
//		for (int i = 0; i < config.getMonths(); i++) {
//			sum += this.getRandom(config.getRangeInf(), config.getRangeSup());
//		}
//		int average = (int)(sum/config.getMonths());
//		return average;
//	}
//	
//	public void setAverage(){
//		for (Iterator iterator = fishShopsList.iterator(); iterator.hasNext();) {
//			Location location = (Location) iterator.next();
//			location.setFishbins(this.getAverage());
//		}
//	}
	
	
	
	
}
